package kr.dhe.gugudan.tu01_gugudan_twonine;

import java.util.ArrayList;
import kr.co.example.newgugudan.R;
import kr.dhe.gugudan.tu01_gugudan_twonine.TwoGugudan.SurView;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.SurfaceHolder.Callback;

public class EigGugudan extends Activity {
	Vibrator mVib;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.tu08_gugudan_twonine);
		setContentView(new SurView(this));
		mVib=(Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	}
	


	public class SurView extends SurfaceView implements Callback {
		Bitmap mBack;
		ArrayList<Ball> arBall;
		final static int DELAY = 50;
		SurfaceHolder mHolder;
		DrawThread mThread;
		
		public SurView(Context context) {
			super(context);
			mBack = BitmapFactory.decodeResource(context.getResources(),R.drawable.eight8);
			
			arBall = new ArrayList<Ball>();
			mHolder = getHolder();
			mHolder.addCallback(this);
		}
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			mThread = new DrawThread(mHolder, arBall);
			mThread.start();
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			if (mThread != null){
				mThread.SizeChange(width, height);
			}
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			mThread.bExit = true;
			for(;;){
				try{
					mThread.join();	//mThread가 종료할때까지 대기
					break;
				}catch(Exception e){}
			}
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN){
				synchronized (mHolder) {
					Ball B = new Ball((int)event.getX(), (int)event.getY());
					mVib.vibrate(500);
					arBall.add(B);
				}
				return true;
			}
			return false;
		
		}
		
		public class DrawThread extends Thread {
			boolean bExit;
			int mWidth;
			int mHeight;
			SurfaceHolder mHolder;
			ArrayList<Ball>arBall;
			
			public DrawThread(SurfaceHolder mHolder, ArrayList<Ball> arBall) {
				super();
				this.mHolder = mHolder;
				this.arBall = arBall;
			}
			public void SizeChange(int width,int height){
				this.mWidth=width;
				this.mHeight=height;
			}
			@Override
			public void run() {
				Canvas canvas;
				Ball B;
				
				while(bExit==false){
					for(int i=0;i<arBall.size();i++){
						B=arBall.get(i);
						B.Move(mWidth,mHeight);
						if(B.count>4){
							arBall.remove(i);
							i--;
						}
					}//end for
					synchronized (mHolder) {
						
						canvas=mHolder.lockCanvas();
						if(canvas==null)break;
						canvas.drawColor(Color.LTGRAY);
						Rect dst = new Rect(0, 0, getWidth(), getHeight());
						canvas.drawBitmap(mBack, null, dst, null);
						for(int i=0;i<arBall.size();i++){
							arBall.get(i).Draw(canvas);
							if(bExit)break;
						}
						mHolder.unlockCanvasAndPost(canvas); //메모리에 있는 그림을 Canvas에 그림
						
				   }// end sync
					
					try{
						sleep(50);
					}catch(Exception e){;}

					}
					
				}//end while
		  
		}

	}
}
